<form class="layui-form seller-form"  action="" >

    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">宿舍楼名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="dormName" lay-verify="title" placeholder="请输入名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>
        
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="dorm-search"><i class="iconfont icon-chaxun"></i>筛选</button>
            <button type="button" class="layui-btn layui-btn-sm add-dorm"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>

    </div>

</form>

<div class="table-body">
    <table id="dormTable" lay-filter="dormTable"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        //执行渲染
        table.render({
            elem: '#dormTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'dormTable',
            url: "{:url('Apartment/dorm')}",
            cols: [[ //标题栏
                {type:'numbers'},
                {field: 'dormName', title: '宿舍楼名', width:200, align:'center'}, 
                // {field: 'num', title: '管理员人数', width:120, align:'center'}, 
                // {field: 'ctime',sort: true, title: '创建时间' ,align:'center', width: 200},
                // {field: 'utime',sort: true, title: '更新时间' ,align:'center', width: 200},
                {field: 'sort', sort: true, title: '排序',align:'center',width: 100},
                {field: 'operating', title: '操作', width:80, align: 'center', templet:function(data){
                    var html = '';
                    html += '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
                    // html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    return html;
                }}
            ]] //设置表头
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        date.render({
            elem: '#utime',
            range: '到'
        });

        form.on('submit(dorm-search)', function(data){
            layui.table.reload('dormTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        $(document).on('click','.add-dorm',function(){
            JsGet("{:url('Apartment/dormAdd')}", function(e){
                if ((e.hasOwnProperty('status') && !e.status) || (e.hasOwnProperty('code') && e.code == 0)) {
                    layer.msg(e.msg); return false;
                }

                window.box = layer.open({
                    type: 1,
                    content: e,
                    area: ['400px', '248px'],
                    title:'添加宿舍楼'
                });
            });
            return false;
        });

        //ajax提交商品的添加
        form.on('submit(add-dorm)', function(data){
            $("#dorm_add_save").addClass("layui-btn-disabled");
            $('#dorm_add_save').attr("disabled", true);

            JsPost("{:url('Apartment/dormAdd')}", data.field, function(res){
                if(res.status){
                    layer.close(window.box);
                    layer.msg(res.msg,{time:1300},function(){
                        table.reload('dormTable');
                    });
                }else{
                    $("#dorm_add_save").removeClass("layui-btn-disabled");
                    $('#dorm_add_save').removeAttr("disabled");

                    layer.msg(res.msg);
                }
            })
        });

        form.on('submit(dorm-edit)',function(data){
            $("#dorm_edit_save").addClass("layui-btn-disabled");
            $('#dorm_edit_save').attr("disabled", true);

            JsPost("{:url('Apartment/dormEdit')}", data.field, function(res){
                if(res.status){
                    layer.close(window.box);
                    layer.msg(res.msg,{time:1300},function(){
                        table.reload('dormTable');
                    });
                }else{
                    $("#dorm_edit_save").removeClass("layui-btn-disabled");
                    $('#dorm_edit_save').removeAttr("disabled");

                    layer.msg(res.msg,{time:1300});
                }
            })
        });

        //监听工具条
        table.on('tool(dormTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsPost("{:url('Apartment/dormDel')}",{id:data.id}, function (res) {
                        if(res.status){
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);//向服务端发送删除指令
                            table.reload('dormTable', {
                            });
                        }
                        layer.msg(res.msg);
                    });
                });
            } else if(layEvent === 'edit'){ //编辑
                JsGet("{:url('Apartment/dormEdit')}?id=" + data.id, function(e){
                    if ((e.hasOwnProperty('status') && !e.status) || (e.hasOwnProperty('code') && e.code == 0)) {
                        layer.msg(e.msg); return false;
                    }

                    window.box = layer.open({
                        type:1,
                        content:e,
                        area: ['400px', '248px'],
                        title:'编辑宿舍楼'
                    })
                });
            }
        });
    })
</script>